keichobot 内部API
code:mod.ts
export type Mode = "normal" | "empathy_writing" | "KPT" | "value_hypothesis";
declare global {
interface Window {
GM_fetch: (typeof fetch) | undefined;
}
}
export const getNewTalkId = async (mode: Mode): Promise<Result<string, ResponseError>> => {
if (!window.GM_fetch) return { ok: false, value: "GM_fetch is not available" };
const res = await window.GM_fetch(${APIROOT}web/create/?mode=${mode}, { mode: "cors" });
if (!res.ok) return { ok: false, value: res };
return { ok: true, value: await res.text() };
};
export type Result<T, E> = { ok: true; value: T } | { ok: false; value: E };
export interface ResponseError {
status: number;
message: string;
}
export interface Answer {
text: string;
buttons: string[];
canInput: boolean;
}
export const ask = async (text: string, talkId: string): Promise<Result<Answer, ResponseError>> => {
if (!window.GM_fetch) return { ok: false, value: "GM_fetch is not available" };
const res = await window.GM_fetch(${APIROOT}web/, {
mode: "cors",
method: "POST",
body: JSON.stringify({ user: "nobody", talk: talkId, text }),
headers: { "Content-Type": "application/json" },
});
if (!res.ok) return { ok: false, value: res };
const data = await res.json();
return { ok: true, value: { text: data.text, buttons: data.buttons, canInput: data.can_input } };
};